<?php
require_once('../lib/converters/converters.php');

class Converter_TestCase extends PHPUnit_TestCase{

	var $object;
	var $namespaces = array(
	    'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
	    'sioc' => 'http://rdfs.org/sioc/ns#',
	    'dc' => 'http://purl.org/dc/elements/1.1/',
	    'terms' => 'http://purl.org/dc/terms/',
	    'foaf' => 'http://xmlns.com/foaf/0.1/',
	    'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
	    'rss' => 'http://purl.org/rss/1.0/',
	    'owl' => 'http://www.w3.org/2002/07/owl#',
	    'rel' => 'http://purl.org/vocab/relationship/',
	    'ibis' => 'http://dannyayers.com/2003/12/ibis.rdfs#',
	    'doap' => 'http://usefulinc.com/ns/doap#',
	    'frbr' => 'http://purl.org/vocab/frbr/core#',
		'xsd' => 'http://www.w3.org/2001/XMLSchema#',
	  );

	var $resources = array (
	  'http://example.org/test/JohnSmith' => 
	  array (
	    'http://xmlns.com/foaf/0.1/name' => 
	    array (
	      0 => 
	      array (
	        'value' => 'John Smith',
	        'type' => 'literal',
	      ),
	    ),
	    'http://xmlns.com/foaf/0.1/mbox' => 
	    array (
	      0 => 
	      array (
	        'value' => 'mailto:john@example.org',
	        'type' => 'uri',
	      ),
	    ),
	    'http://www.w3.org/2000/01/rdf-schema#label' => 
	    array (
	      0 => 
	      array (
	        'value' => 'Label',
	        'type' => 'literal',
	        'lang' => 'en-gb',
	      ),
	    ),
	  ),
	);
	var $form_array_with_uris;
	
	var $resources_from_form;

	var $rdfxml = '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:sioc="http://rdfs.org/sioc/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:terms="http://purl.org/dc/terms/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:rss="http://purl.org/rss/1.0/" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:rel="http://purl.org/vocab/relationship/" xmlns:ibis="http://dannyayers.com/2003/12/ibis.rdfs#" xmlns:doap="http://usefulinc.com/ns/doap#" xmlns:frbr="http://purl.org/vocab/frbr/core#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" ><rdf:Description rdf:about="http://example.org/test/JohnSmith"><foaf:name>John Smith</foaf:name><foaf:mbox  rdf:resource="mailto:john@example.org"/><rdfs:label xml:lang="en-gb">Label</rdfs:label></rdf:Description></rdf:RDF>';
	
	var $sparql_xml = '<sparql
	    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	    xmlns:xs="http://www.w3.org/2001/XMLSchema#"
	    xmlns="http://www.w3.org/2005/sparql-results#" >
	  <head>
	    <variable name="s"/>
	    <variable name="p"/>
	    <variable name="o"/>
	  </head>
	  <results ordered="false" distinct="true">
	    <result>
	      <binding name="s">

	        <uri>http://api.talis.local/bf/stores/engagetenantstore/items/1173364330999#self</uri>
	      </binding>
	      <binding name="p">
	        <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#subject</uri>
	      </binding>
	      <binding name="o">
	        <uri>http://api.talis.local/bf/stores/engagetenantstore/items/1174262688178#self</uri>

	      </binding>
	    </result>
	    <result>
	      <binding name="s">
	        <uri>http://api.talis.local/bf/stores/engagetenantstore/items/1173364330999#self</uri>

	      </binding>
	      <binding name="p">
	        <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#object</uri>
	      </binding>
	      <binding name="o">
	        <literal>1a3c47c9-fb29-4fd2-a061-a3b72328c96b</literal>
	      </binding>
	    </result>

	  </results>
	</sparql>';

	var $select_array = array(
	  0 => 
	  array(
	    's' => 
	    array(
	      'type' => 'uri',
	      'value' => 'http://api.talis.local/bf/stores/engagetenantstore/items/1173364330999#self',
	    ),
	    'p' => 
	    array(
	      'type' => 'uri',
	      'value' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#subject',
	    ),
	    'o' => 
	    array(
	      'type' => 'uri',
	      'value' => 'http://api.talis.local/bf/stores/engagetenantstore/items/1174262688178#self',
	    ),
	  ),
	  1 => 
	  array(
	    's' => 
	    array(
	      'type' => 'uri',
	      'value' => 'http://api.talis.local/bf/stores/engagetenantstore/items/1173364330999#self',
	    ),
	    'p' => 
	    array(
	      'type' => 'uri',
	      'value' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#object',
	    ),
	    'o' => 
	    array(
	      'type' => 'literal',
	      'value' => '1a3c47c9-fb29-4fd2-a061-a3b72328c96b',
	    ),
	  ),
	);
	
	var $sparql_true = '<sparql>
		<head></head>
		<results>
			<boolean>TRUE</boolean>
		</results>
	</sparql>';
	
	var $sparql_false = '<sparql>
		<head></head>
		<results>
			<boolean>FALSE</boolean>
		</results>
	</sparql>';

	var $turtle = '
<http://example.org/test/JohnSmith>  <http://xmlns.com/foaf/0.1/name> "John Smith" ; 
        <http://xmlns.com/foaf/0.1/mbox> <mailto:john@example.org> ; 
        <http://www.w3.org/2000/01/rdf-schema#label> "Label"@en-gb . 

			  ';
	
	function __construct($name)
	{
		$this->PHPUnit_TestCase($name);
		$this->form_array_with_uris = array(
				'prefixes' => $this->namespaces,
				'resources' => array(
					'http://example.org/test/JohnSmith' => array(
						'foaf:name' => 'John Smith',
		    			'rdfs:label@en-gb' => 'Label',
						'foaf:mbox' => 'mailto:john@example.org',
						'foaf:nick' => array('johnno','johnsmith'),
						'foaf:age^^xsd:integer' => 21,
						),
					'foaf:Person' => array(
						'foaf:name' => 'John Smith',
						'rdfs:label@en-gb' => 'Label',
						'foaf:mbox' => 'mailto:john@example.org',
						'foaf:nick' => array('johnno','johnsmith'),
						'foaf:age^^xsd:integer' => 21,
						),
					),
			);

		$properties = array_shift(array_values($this->resources));
		$bnode_resources = array('_:bnode1' => $properties);
	   	$this->resources_from_form = array_merge_recursive($this->resources);
	}
	
	function setUp(){
		$this->object = new Converter($this->namespaces);
	}
	
	function tearDown(){
		unset($this->f);
	}
	
	
	function test_RDFXML_to_resources() {
		$ex = $this->resources;
		$act = $this->object->RDFXML->to_resources($this->rdfxml)->value();
		$this->assertEquals($ex, $act);		
	}
	function test_Resources_to_rdfxml() {
		$rdf = <<<_RDF_
			<rdf:RDF
			    xmlns:foaf="http://xmlns.com/foaf/0.1/"
			    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
			    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
			    xmlns:j.0="http://schemas.talis.com/2005/dir/schema#"
			    xmlns:j.1="http://xmlns.com/wot/0.1/"
			    xmlns:dc="http://purl.org/dc/elements/1.1/" > 
			  <rdf:Description rdf:about="http://eikeon.com/foaf.rdf">
			    <rdfs:seeAlso>http://donnafales.com/foaf.rdf</rdfs:seeAlso>
			    <rdf:type>http://xmlns.com/foaf/0.1/Document</rdf:type>
			    <rdfs:seeAlso>http://heddley.com/edd/foaf.rdf</rdfs:seeAlso>
			    <rdfs:label>eikeon's FOAF document</rdfs:label>
			    <rdfs:seeAlso>http://xmlns.com/foaf/0.1/</rdfs:seeAlso>

			    <j.1:assurance>http://eikeon.com/foaf.rdf.asc</j.1:assurance>
			    <rdfs:comment>Document with Friend-of-a-Friend description of eikeon.

			edited</rdfs:comment>
			    <j.0:etag>6c35f4ed-db6f-48fe-b871-a41ae3165ead</j.0:etag>
			    <rdfs:seeAlso>http://www.perceive.net/xml/foaf.rdf</rdfs:seeAlso>
			    <rdfs:seeAlso>http://iama.rrecktek.com/~rreck/foaf.rdf</rdfs:seeAlso>
			  </rdf:Description>

			</rdf:RDF>
_RDF_;
		$ex = $this->object->RDFXML->to_triples($rdf)->value();
		$act = $this->object->RDFXML->to_triples($this->object->RDFXML->to_resources($rdf)->to_rdfxml())->value();
		$this->assertEquals(count($ex), count($act));		
	}
	
	function test_Resources_to_turtle() {
		$act = preg_replace('/\s/','',$this->object->RDFXML->to_resources($this->rdfxml)->to_turtle());
		$ex = preg_replace('/\s/','',$this->turtle);
		$this->assertEquals($ex, $act);		
	}
	
	function test_sparqlxml_to_array()
	{
		$ex = $this->select_array;
		$act = $this->object->SPARQL_XML->to_array($this->sparql_xml);
		$this->assertEquals($ex, $act);		
	}
	
	function test_sparqlxml_to_boolean_true()
	{
		$ex = 'true';
		$act = $this->object->SPARQL_XML->to_boolean($this->sparql_true);
		$this->assertEquals($ex, $act);		
	}
	
	function test_sparqlxml_to_boolean_false()
	{
		$ex = 'false';
		$act = $this->object->SPARQL_XML->to_boolean($this->sparql_false);
		$this->assertEquals($ex, $act);		
	}
	
	function test_Qname_static_to_term()
	{
		$ex = 'name';
		$act = Qname::to_term('foaf:name');
		$this->assertEquals($ex,$act);		
	}
	
	function test_Qname_to_term()
	{
		$ex = 'name';
		$act = $this->object->Qname->to_term('foaf:name');
		$this->assertEquals($ex,$act);
	}
	
	function test_Qname_static_to_uri()
	{
		$ex = new URI('http://xmlns.com/foaf/0.1/name');
		$act = Qname::to_uri('foaf:name', $this->namespaces);
		$this->assertTrue($act->value()===$ex->value());
	}
	
	function test_Qname_to_uri()
	{
		$ex = new URI('http://xmlns.com/foaf/0.1/name');
		$act = $this->object->Qname->to_uri('foaf:name');
		$this->assertTrue($act->value()===$ex->value());
	}
	
	function test_Qname_static_to_eRDF_term()
	{
		$ex = 'foaf-Person';
		$act = Qname::to_eRDF_term('foaf:Person');
		$this->assertEquals($ex,$act);	}
	
	function test_Qname_to_eRDF_term()
	{
		$ex = 'foaf-Person';
		$qname = new Qname('foaf:Person');
		$act = $qname->to_eRDF_term();
		$this->assertEquals($ex,$act);	
	}
	
	function test_URI_to_eRDF_term()
	{
		$act= $this->object->URI->to_eRDF_term('http://xmlns.com/foaf/0.1/name');
		$ex = 'foaf-name';
		$this->assertEquals($ex,$act);	
	}
	function test_URI_to_namespace()
	{
		$act= $this->object->URI->to_namespace('http://xmlns.com/foaf/0.1/name');
		$ex = 'http://xmlns.com/foaf/0.1/';
		$this->assertEquals($ex,$act);	
	}
	function test_URI_to_qname()
	{
		$act= $this->object->URI->to_qname('http://xmlns.com/foaf/0.1/name')->value();
		$ex = 'foaf:name';
		$this->assertEquals($ex,$act);	
	}
	function test_URI_to_term()
	{
		$act= $this->object->URI->to_term('http://xmlns.com/foaf/0.1/name');
		$ex = 'name';
		$this->assertEquals($ex,$act);	
	}
	function test_URI_split()
	{
		$act= $this->object->URI->split('http://xmlns.com/foaf/0.1/name');
		$ex = array('http://xmlns.com/foaf/0.1/','name');
		$this->assertEquals($ex,$act);	
	}
	
	function test_Resource_get_title(){
		$Resource = new ResourceArray($this->resources);
		$ex = 'John Smith';
		$act = $Resource->get_title();
		$nothing = $Resource->property('http://xmlns.com/foaf/0.1/name');
		$this->assertTrue(($ex==$act) && $nothing===false);	
		
	}
	
	function test_Resources_get_languages(){
		$ex = array('en-gb');
		$act = $this->object->GraphIndex->get_languages($this->resources);
		$this->assertEquals($ex,$act);	
	}
	
	function test_Resources_to_resource()
	{
		$r = $this->resources;
		$resources = array(array_shift(array_keys($r)) => array_shift(array_values($r)));
		$ex = new ResourceArray($resources, $this->namespaces);
		$Resources = new GraphIndex($this->resources, $this->namespaces);
		$act = $Resources->to_resource(array_shift(array_keys($r)));
		$this->assertEquals($ex->value(),$act->value());	
	}
	
	function test_Resource_property(){
		$Resources = new GraphIndex($this->resources, $this->namespaces);
		$r = $this->resources;
		$Res = $Resources->to_resource(array_shift(array_keys($r)));
		$act = $Res->property('http://xmlns.com/foaf/0.1/name');
		$ex = $this->resources['http://example.org/test/JohnSmith']['http://xmlns.com/foaf/0.1/name'][0]['value'];
		$this->assertEquals($ex,$act);	
		
	}
	
	function test_Resource_property_unset(){
		$Resources = new GraphIndex($this->resources, $this->namespaces);
		$r = $this->resources;
		
		$Res = $Resources->to_resource(array_shift(array_keys($r)));
		$name = $Res->property('http://xmlns.com/foaf/0.1/name');
		$act = $Res->property('http://xmlns.com/foaf/0.1/name');
		$ex = false;
		$this->assertEquals($ex,$act);	
		
	}
	
	function test_Resources_filter_by_language_en_gb(){
		$Resources = new GraphIndex($this->resources);
		$ex = $this->resources;
		$act = $Resources->filter_by_language('en')->value();
		$this->assertEquals($ex,$act);		
	}
	
	function test_formsArray_to_resourcesArray(){
		$ex = array(
		  'http://example.org/test/JohnSmith' => 
		  array(
		    'http://xmlns.com/foaf/0.1/name' => 
		    array(
		      0 => 
		      array(
		        'value' => 'John Smith',
		        'type' => 'literal',
		      ),
		    ),
		    'http://www.w3.org/2000/01/rdf-schema#label' => 
		    array(
		      0 => 
		      array(
		        'value' => 'Label',
		        'type' => 'literal',
		        'lang' => 'en-gb',
		      ),
		    ),
		    'http://xmlns.com/foaf/0.1/mbox' => 
		    array(
		      0 => 
		      array(
		        'value' => 'mailto:john@example.org',
		        'type' => 'literal',
		      ),
		    ),
		    'http://xmlns.com/foaf/0.1/nick' => 
		    array(
		      0 => 
		      array(
		        'value' => 'johnno',
		        'type' => 'literal',
		      ),
		      1 => 
		      array(
		        'value' => 'johnsmith',
		        'type' => 'literal',
		      ),
		    ),
		    'http://xmlns.com/foaf/0.1/age' => 
		    array(
		      0 => 
		      array(
		        'value' => 21,
		        'type' => 'literal',
		        'datatype' => 'xsd:integer',
		      ),
		    ),
		  ),
		  '_:bnode1' => 
		  array(
		    'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' => 
		    array(
		      0 => 
		      array(
		        'type' => 'uri',
		        'value' => 'http://xmlns.com/foaf/0.1/Person',
		      ),
		    ),
		    'http://xmlns.com/foaf/0.1/name' => 
		    array(
		      0 => 
		      array(
		        'value' => 'John Smith',
		        'type' => 'literal',
		      ),
		    ),
		    'http://www.w3.org/2000/01/rdf-schema#label' => 
		    array(
		      0 => 
		      array(
		        'value' => 'Label',
		        'type' => 'literal',
		        'lang' => 'en-gb',
		      ),
		    ),
		    'http://xmlns.com/foaf/0.1/mbox' => 
		    array(
		      0 => 
		      array(
		        'value' => 'mailto:john@example.org',
		        'type' => 'literal',
		      ),
		    ),
		    'http://xmlns.com/foaf/0.1/nick' => 
		    array(
		      0 => 
		      array(
		        'value' => 'johnno',
		        'type' => 'literal',
		      ),
		      1 => 
		      array(
		        'value' => 'johnsmith',
		        'type' => 'literal',
		      ),
		    ),
		    'http://xmlns.com/foaf/0.1/age' => 
		    array(
		      0 => 
		      array(
		        'value' => 21,
		        'type' => 'literal',
		        'datatype' => 'xsd:integer',
		      ),
		    ),
		  ),
		);
		$act = $this->object->FormArray->to_resources($this->form_array_with_uris)->value();
		$this->assertEquals($ex,$act);	
	}

	function test_formsArray_parse_property_lang(){
		$ex = array('lang'=>'en-gb','datatype'=>false,'property'=>'rdfs:label');
		$act = $this->object->FormArray->parse_property('rdfs:label@en-gb');
		$this->assertEquals($ex,$act);	
	}
	function test_formsArray_parse_property_datatype(){
		$ex = array('lang'=>false,'datatype'=>'xsd:string','property'=>'rdfs:label');
		$act = $this->object->FormArray->parse_property('rdfs:label^^xsd:string');
		$this->assertEquals($ex,$act);	
	}	
	
	function test_formsArray_parse_property(){
		$ex = array('lang'=>false,'datatype'=>false,'property'=>'rdfs:label');
		$act = $this->object->FormArray->parse_property('rdfs:label');
		$this->assertEquals($ex,$act);	
	}
	
	function test_resources_to_exhibit(){
		var_dump($this->GraphIndex->to_exhibit($this->resources));
	}
}





?>
